import { getDetailInfo } from "api/api";
export default {
    state: {
        detailInfo: {}
    },
    getters: {
        categoryView(state) {
            return state.detailInfo.categoryView ? state.detailInfo.categoryView : {}
        },
        skuInfo(state) {
            return state.detailInfo.skuInfo ? state.detailInfo.skuInfo : {}
        },
        spuSaleAttrList(state) {
            return state.detailInfo.spuSaleAttrList ? state.detailInfo.spuSaleAttrList : []
        },
        //第二个参数是getter计算属性
        propsChecked(state, getters) {
            let arr = []
            getters.spuSaleAttrList.forEach(item => {
                item.spuSaleAttrValueList.forEach(value => {
                    if (value.isChecked === '1') {
                        arr.push({
                            ...value,
                            attrId: item.id
                        })
                    }
                })
            })
            return arr;
        }
    },
    mutations: {
        getDetailInfo(state, detailInfo) {
            state.detailInfo = detailInfo
        },
        //修改属性选中
        checked(state, { attrId, attrValueId }) {
            state.detailInfo.spuSaleAttrList.forEach(item => {
                if (item.id === attrId) {
                    item.spuSaleAttrValueList.forEach(value => {
                        value.isChecked = '0'
                        if (value.id === attrValueId) {
                            value.isChecked = '1'
                        }
                    })
                }
            });
        }
    },
    actions: {
        async getDetailInfo(store, skuId) {
            try {
                const { code, data: detailInfo } = await getDetailInfo(skuId)
                if (code === 200) {
                    store.commit('getDetailInfo', detailInfo)
                }
                return { code, data: detailInfo }
            } catch (error) {
                return Promise.reject(error)
            }
        }
    }
}